package com.tanhua.manage.listener;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserInfoApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
  public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
    super(listenerContainer);
  }

  @Reference
  private UserInfoApi userInfoApi;

  @Autowired
  private RedisTemplate redisTemplate;

  @Override
  public void onMessage(Message message, byte[] pattern) {
    String expiredKey = message.toString();
    log.info("expiredKey=========" + expiredKey);
      String key = expiredKey.split("_")[0];
      if("FREEZE".equals(key)&&!redisTemplate.hasKey(expiredKey)){
      String userId = expiredKey.split("_")[2];
      UserInfo userInfo = userInfoApi.findById(Long.parseLong(userId));
      userInfo.setUserStatus("1");
      userInfoApi.update(userInfo);
    }
  }
}